package com.awaken.content.service.dubbo.impl;

import com.awaken.content.models.dto.ProductDTO;
import com.awaken.content.models.entity.Product;
import com.awaken.content.models.request.ProductRequest;
import com.awaken.content.mapper.ProductMapper;
import com.awaken.content.service.dubbo.ProductDubboService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品Dubbo服务实现类
 *
 * @author awaken-lin
 */
@Slf4j
@Service
public class ProductDubboServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductDubboService {

    @Override
    public ProductDTO createProduct(ProductRequest request) {
        log.info("创建商品: {}", request.getName());
        
        Product product = new Product();
        product.setName(request.getName());
        product.setDescription(request.getDescription());
        product.setCategoryId(request.getCategoryId());
        product.setPrice(new java.math.BigDecimal(request.getPrice()));
        product.setStock(request.getStock());
        product.setStatus(1);
        product.setCreateTime(LocalDateTime.now());
        
        this.save(product);
        
        ProductDTO productDTO = new ProductDTO();
        BeanUtils.copyProperties(product, productDTO);
        
        log.info("商品创建成功: {}", productDTO);
        return productDTO;
    }

    @Override
    public ProductDTO getProductById(Long id) {
        log.info("获取商品信息: {}", id);
        
        Product product = this.getById(id);
        if (product == null) {
            log.warn("商品不存在: {}", id);
            return null;
        }
        
        ProductDTO productDTO = new ProductDTO();
        BeanUtils.copyProperties(product, productDTO);
        return productDTO;
    }

    @Override
    public List<ProductDTO> getAllProducts() {
        log.info("获取所有商品列表");
        
        List<Product> products = this.list();
        return products.stream()
                .map(product -> {
                    ProductDTO productDTO = new ProductDTO();
                    BeanUtils.copyProperties(product, productDTO);
                    return productDTO;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<ProductDTO> getProductsByCategory(Long categoryId) {
        log.info("获取分类商品列表: {}", categoryId);
        
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        queryWrapper.eq("status", 1);
        
        List<Product> products = this.list(queryWrapper);
        return products.stream()
                .map(product -> {
                    ProductDTO productDTO = new ProductDTO();
                    BeanUtils.copyProperties(product, productDTO);
                    return productDTO;
                })
                .collect(Collectors.toList());
    }

    @Override
    public boolean updateStock(Long productId, Integer amount) {
        log.info("更新商品库存: productId={}, amount={}", productId, amount);
        
        Product product = this.getById(productId);
        if (product == null) {
            log.warn("商品不存在: {}", productId);
            return false;
        }
        
        if (product.getStock() < amount) {
            log.warn("库存不足: productId={}, stock={}, amount={}", productId, product.getStock(), amount);
            return false;
        }
        
        product.setStock(product.getStock() - amount);
        product.setUpdateTime(LocalDateTime.now());
        
        return this.updateById(product);
    }
} 